var express = require("express");
var router = express.Router();
const getTime = require("../../utils/getTime");
const roleModel = require("../../models/roleModel");

// 获取菜单列表
router.get("/menuList", (req, res) => {
  let data = [
    {
      id: "1001",
      label: "商品管理",
      name: "Product",
      disabled: false,
      children: [
        {
          id: "10010",
          label: "农产品管理",
          name: "Agricultural",
          disabled: false,
        },
        {
          id: "10011",
          label: "特色农产品管理",
          name: "Characteristic",
          disabled: false,
        },
      ],
    },
    {
      id: "1002",
      label: "信息管理",
      name: "Information",
      disabled: false,
      children: [
        {
          id: "10020",
          label: "广告管理",
          name: "Ad",
          disabled: false,
        },
        {
          id: "10021",
          label: "助农资讯",
          name: "Message",
          disabled: false,
        },
        {
          id: "10022",
          label: "助农政策",
          name: "Policy",
          disabled: false,
        },
        {
          id: "10023",
          label: "订单管理",
          name: "Order",
          disabled: false,
        },
      ],
    },
    {
      id: "1003",
      label: "权限管理",
      name: "Auth",
      disabled: false,
      children: [
        {
          id: "10030",
          label: "用户管理",
          name: "User",
          disabled: false,
        },
        {
          id: "10031",
          label: "账号管理",
          name: "Account",
          disabled: false,
        },
        {
          id: "10032",
          label: "角色管理",
          name: "Permission",
          disabled: false,
        },
      ],
    },
  ];
  return res.json({
    code: 200,
    data,
    msg: "获取菜单列表成功~",
  });
});

// 获取角色列表
router.get("/roles", (req, res) => {
  let { pageSize, pageNo } = req.query;
  if (pageSize == 'undefined' || pageNo == 'undefined') {
    roleModel
      .find()
      .then((data) => {
        if (data.length == 0) {
          return res.json({
            code: 200,
            data,
            msg: "数据为空",
          });
        } else {
          return res.json({
            code: 200,
            data,
            msg: "成功获取数据~",
          });
        }
      })
      .catch((err) => {
        res.status(500).json({ message: err.message });
      });
  } else {
    roleModel
      .find()
      .skip((pageNo - 1) * pageSize)
      .limit(pageSize)
      .then((data) => {
        if (data.length == 0) {
          return res.json({
            code: 200,
            data,
            msg: "数据为空",
          });
        } else {
          return res.json({
            code: 200,
            data,
            msg: "成功获取数据~",
            total: data.length,
          });
        }
      })
      .catch((err) => {
        res.status(500).json({ message: err.message });
      });
  }
});

// 新增角色
router.post("/addRole", (req, res) => {
  let createdAt = getTime(new Date());
  roleModel
    .create({ ...req.body, createdAt })
    .then((data) => {
      if (!data) {
        return res.json({
          code: -1,
          msg: "新增失败",
          data: null,
        });
      }
      return res.json({
        code: 200,
        msg: "新增成功",
        data: data,
      });
    })
    .catch((err) => {
      res.status(500).json({ message: err.message });
    });
});

// 修改角色
router.post("/updateRole", (req, res) => {
  let { _id } = req.body;
  roleModel
    .updateOne({ _id }, { ...req.body })
    .then((data) => {
      if (!data.acknowledged) {
        return res.json({
          code: -1,
          msg: "修改失败",
          data: null,
        });
      }
      return res.json({
        code: 200,
        msg: "修改成功~",
        data: null,
      });
    })
    .catch((err) => {
      res.status(500).json({ message: err.message });
    });
});

// 删除角色
router.post("/deleteRole", (req, res) => {
  let { _id } = req.body;
  roleModel
    .deleteOne({ _id })
    .then((result) => {
      if (result.deletedCount !== 0) {
        return res.json({
          code: 200,
          msg: "删除成功",
          data: null,
        });
      }
      return res.json({
        code: -1,
        msg: "删除失败",
        data: null,
      });
    })
    .catch((err) => {
      res.status(500).json({ message: err.message });
    });
});

// 搜索角色
router.get("/searchRole", (req, res) => {
  let { roleName } = req.query;
  const regex = new RegExp(roleName);
  roleModel
    .find({ roleName: regex })
    .then((data) => {
      if (data.length === 0) {
        return res.json({
          code: 200,
          msg: "角色信息为空",
          data: data,
        });
      }
      return res.json({
        code: 200,
        msg: "查询成功",
        data: data,
        total: data.length,
      });
    })
    .catch((err) => {
      res.status(500).json({ message: err.message });
    });
});

module.exports = router;
